/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * MainPane.java 2008-11-10
 */
package ie.ucc.cs.anggao.finalproject.client.ui.mainpanel;

import ie.ucc.cs.anggao.finalproject.client.support.blogPost.Poster;
import ie.ucc.cs.anggao.finalproject.client.support.event.ApplicationEvent;
import ie.ucc.cs.anggao.finalproject.client.support.event.ApplicationEventListener;
import ie.ucc.cs.anggao.finalproject.client.ui.BasePane;
import ie.ucc.cs.anggao.finalproject.client.ui.event.PostEvent;
import ie.ucc.cs.anggao.finalproject.client.ui.event.QueryBackEvent;
import ie.ucc.cs.anggao.finalproject.client.ui.event.QueryEvent;
import ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.editorpane.EditorPane;
import ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane.InfoPane;

import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;

/**
 * @author GaoAng
 * 
 */
public class MainPane extends BasePane {

	private HeaderPane headerPane;
	private StatusBarPane statusBarPane;
	private EditorPane editorPane;
	private InfoPane infoPane;

	public MainPane() {

		DockPanel topPanel = new DockPanel();
		topPanel.setBorderWidth(5);
		topPanel.setSize("100%", "100%");
		
		headerPane = new HeaderPane("Just In Time Information Retrivial Editor");
		topPanel.add(headerPane, DockPanel.NORTH);
		topPanel.setCellHeight(headerPane, "30px");
		topPanel.setCellHorizontalAlignment(headerPane, DockPanel.ALIGN_CENTER);
		topPanel.setCellVerticalAlignment(headerPane, DockPanel.ALIGN_MIDDLE);

		statusBarPane = new StatusBarPane();
		topPanel.add(statusBarPane, DockPanel.SOUTH);
		topPanel.setCellHeight(statusBarPane, "25px");
		topPanel.setCellHorizontalAlignment(statusBarPane,
				DockPanel.ALIGN_CENTER);
		topPanel
				.setCellVerticalAlignment(statusBarPane, DockPanel.ALIGN_MIDDLE);

		HorizontalSplitPanel editorAndInfo = new HorizontalSplitPanel();
		editorAndInfo.setSplitPosition("300px");

		editorPane = new EditorPane();
		editorAndInfo.setRightWidget(editorPane);
		infoPane = new InfoPane();
		infoPane.addListener(new QueryBackEventListener());
		editorAndInfo.setLeftWidget(infoPane);

		topPanel.add(editorAndInfo, DockPanel.CENTER);

		editorPane.addListener(new QueryEventListener());
		editorPane.addListener(new PostEventListener());
		
		initWidget(topPanel);

	}

	// ============================================= Inner Classes======================================================

	protected class QueryEventListener implements ApplicationEventListener {
		public void handle(ApplicationEvent event) {
			if (event instanceof QueryEvent) {
				infoPane.InformationRetrivial(((QueryEvent) event));
//				System.out.println(("HTML is ---"+((QueryEvent) event).getHTML()));
//				statusBarPane.setMessage(event.getDescription());
			}
		}
	}
	
	protected class QueryBackEventListener implements ApplicationEventListener {
		public void handle(ApplicationEvent event) {
			if (event instanceof QueryBackEvent) {
				statusBarPane.setMessage(event.getDescription());
			}
		}
	}

	
	protected class PostEventListener implements ApplicationEventListener {
		public void handle(ApplicationEvent event) {
			if (event instanceof PostEvent) {
				
				((PostEvent) event).getModel().setContent(editorPane.getAreaText());
				((PostEvent) event).getModel().setTitle(editorPane.getTitleText());
				Poster poster = new Poster(((PostEvent) event).getModel());
				poster.PostEntity();
				statusBarPane.setMessage(event.getDescription());
			}
		}
	}
	
}
